﻿using AutoMapper;
using Inventory.API.Application.Command.ZonemanagementCommand;
using Inventory.Errorcode;
using Inventory.Errorcode.ErrorCode;
using Inventory.Infrastructure.Interfaces;
using MediatR;

namespace Inventory.API.Application.CommandHandler.ZonemanagementCommandHandler
{
    public class UpdateZonemanagementCommandHanlder : IRequestHandler<UpdateZonemanagementCommand, ApiResult>
    {

        private readonly IZonemanagementRepository repository;
        private readonly ILogger<UpdateZonemanagementCommandHanlder> logger;

        private readonly IMapper mapper;

        public UpdateZonemanagementCommandHanlder(IZonemanagementRepository repository, ILogger<UpdateZonemanagementCommandHanlder> logger, IMapper mapper)
        {
            this.repository = repository;
            this.logger = logger;
            this.mapper = mapper;
        }
        public async Task<ApiResult> Handle(UpdateZonemanagementCommand request, CancellationToken cancellationToken)
        {

            try
            {
                var zonemanagement = await repository.GetAsync(x => x.Zonemanagementid == request.Zonemanagementid);

                if (zonemanagement == null)
                {

                    return ApiResult.Fail("未找到id", StatusCodeEnum.NotFound);
                }
                else
                {
                    var updatewarehousing = mapper.Map(request, zonemanagement);
                    await repository.UpdateAsync(updatewarehousing);
                    return ApiResult.Success(StatusCodeEnum.Success);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message, "Error occured while updating zonemanagement");
                throw;
            }
        }
    }
}
